package cn.shop.ylp.controll;

import cn.shop.ylp.Orderform;
import cn.shop.ylp.OrderformMapper;
import cn.shop.ylp.PubFun;
import cn.shop.ylp.error.CommonReturnType;
import cn.shop.ylp.service.ImgS;
import cn.shop.ylp.service.Unique;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Hashtable;

/**
 * @author yulianpeng
 * @email network_u@163.com
 * Date 2020/9/13
 */
@Controller
@RequestMapping("/order")
@CrossOrigin(allowCredentials = "true",allowedHeaders = "*")
public class OrderFormC {
    @Autowired
    private OrderformMapper orderformMapper;
    @Autowired
    private Unique uniqueS;
    @RequestMapping(value = "/create",method = {RequestMethod.POST})
    @ResponseBody
    public CommonReturnType create(@RequestParam(name = "userid") String userid,
            @RequestParam(name = "name") String name,
            @RequestParam(name = "goodsid") String goodsid,
            @RequestParam(name = "sizesq") String sizesq,
            @RequestParam(name = "nums") String nums,
            @RequestParam(name = "price") String price,
            @RequestParam(name = "money") String money,
            @RequestParam(name = "address") String address,
            @RequestParam(name = "phone") String phone){
        Orderform orderform = new Orderform();
        orderform.setPhone(phone);
        orderform.setAddressno(address);
        orderform.setName(name);
        orderform.setUserId(userid);
        orderform.setShopId(goodsid);
        orderform.setSizeSq(sizesq);
        orderform.setGoodsId(goodsid);
        orderform.setNums(nums);
        orderform.setPrice(price);
        orderform.setMoney(money);
        //未支付
        orderform.setPay(0);
        orderform.setOrderno(generOrderNo());
        int tr = orderformMapper.insert(orderform);
        if (tr>0){
            return CommonReturnType.create(orderform);
        } else {
            return CommonReturnType.create(null,"fail");
        }
    }
    @RequestMapping(value = "/order",method = {RequestMethod.POST})
    @ResponseBody
    public CommonReturnType getMoney(@RequestParam(name = "orderno") String orderno){
        Orderform orderform = orderformMapper.selectByPrimaryKey(orderno);
        return CommonReturnType.create(orderform);
    }
    public String generOrderNo(){
        String date = PubFun.cuurDate().replace("-","");
        String time = PubFun.cuurTime().replace(":","");
        String max = uniqueS.getFourMax();
        while (max.length()<4){
            max = "0"+max;
        }
        String no = date+time+max;
        return no;
    }
    @RequestMapping(value = "/qrimage",method = {RequestMethod.GET})
    @ResponseBody
    public ResponseEntity<byte[]> getQRImage(@RequestParam(name = "money") String money) {

        //二维码内的信息
        String info = "你需要支付"+money+"元！";

        byte[] qrcode = null;
        try {
            qrcode = ImgS.getQRCodeImage(info, 159, 153);
        } catch (WriterException e) {
            System.out.println("Could not generate QR Code, WriterException :: " + e.getMessage());
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("Could not generate QR Code, IOException :: " + e.getMessage());
        }
        // Set headers
        final HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.IMAGE_PNG);

        return new ResponseEntity<byte[]> (qrcode, headers, HttpStatus.CREATED);
    }


}
